﻿Library.UserInterface.Widgets.Calendar = function (options) {
    var defaults = {
        container: null,                
        titleSelector: "",
        onSettingsSave: ""
    };
    var options = $.extend(defaults, options);

    this.container = options.container;
    this.titleSelector = options.titleSelector;
    this.onSettingsSave = options.onSettingsSave;

    this.initialize(false);
}

Library.UserInterface.Widgets.Calendar.prototype = {
    container: null,
    titleSelector: "",
    onSettingsSave: "",
    fullscreen: true,
    minimize: true,
    settings: false,
    id: null,
    type: "widget.calendar",

    initialize: function (set) {
        if (this.id != null)
            Library.UserInterface.Widgets.Dispose(this.id);

        this.id = Library.Util.ReplaceAll(Library.Crypto.Guid(), "-", "");
        this.container.find(".portlet-widget").html(Library.UserInterface.Widgets.Calendar.Lang.loading);

        Library.UserInterface.Widgets.Add(this);

        if (this.titleSelector == "")
            this.titleSelector = this.container.find(".portlet-title").selector;

        if (this.titleSelector != "")
            $(this.titleSelector).html(Library.UserInterface.Widgets.Calendar.Lang.loading);

        try {
            this.container.find(".portlet-widget").html("");
            var widget = this;
            this.container.find(".portlet-widget").fullCalendar({
                editable: true,
                timeFormat: 'HH(:mm)',
                axisFormat: 'HH:mm',
                slotMinutes: 15,
                header: {
                    left: 'prev,today,next',
                    center: 'title',
                    right: 'month,agendaWeek,agendaDay'
                },
                events: "/Cloud/KullaniciAjanda.aspx?islem=Json",

                eventClick: function (event) {
                    widget.getForm(event);
                },
                eventDrop: function (event) {
                    widget.saveEvent(widget, event.id, $.fullCalendar.formatDate(event._start, 'dd.MM.yyyy'), $.fullCalendar.formatDate(event._start, 'HH:mm'),
                        $.fullCalendar.formatDate(event._end, 'dd.MM.yyyy'), $.fullCalendar.formatDate(event._end, 'HH:mm'), event.title);
                },
                eventResize: function (event) {
                    widget.saveEvent(widget, event.id, $.fullCalendar.formatDate(event._start, 'dd.MM.yyyy'), $.fullCalendar.formatDate(event._start, 'HH:mm'),
                        $.fullCalendar.formatDate(event._end, 'dd.MM.yyyy'), $.fullCalendar.formatDate(event._end, 'HH:mm'), event.title);
                },
                eventRender: function (event, element) {
                    if (event.start < new Date()) {
                        element.find(".fc-event-title").css("text-decoration", "line-through");                        
                    }
                },
                loading: function (bool) {
                    if (bool) {
                        $(widget.titleSelector).html(Library.UserInterface.Widgets.Calendar.Lang.loading);
                    } else {
                        $(widget.titleSelector).html(Library.UserInterface.Widgets.Calendar.Lang.title);
                    }
                },
                selectable: true,
                select: function (start, end, allDay) {
                    widget.createForm('', start, end, '');
                }
            });

            Library.UserInterface.Widgets.SetHeaderIcons(this);
            $(this.titleSelector).html(Library.UserInterface.Widgets.Calendar.Lang.title);
        } catch (err) {
            Library.UserInterface.Widgets.Error(this.id, err);
        }
    },

    getForm: function (event) {
        this.createForm(event.id, event.start, event.end, event.title);
    },

    createForm: function (id, start, end, title) {
        var widget = this;

        $(".CalendarForm").remove();

        $(document.body).append("<div class=\"CalendarForm\" title=\"\"></div>");
        $(".CalendarForm").append("<input type='hidden' name='ajandaId' id='ajandaId' value='" + id + "'/>");
        $(".CalendarForm").append("<label >Başlangıç Tarihi</label>");
        $(".CalendarForm").append("<input type='text' name='baslangicTarihi' class='Date' id='baslangicTarihi' value='" + $.fullCalendar.formatDate(start, 'dd.MM.yyyy') + "'/>");
        $(".CalendarForm").append("<input type='text' name='baslangicSaati' class='Time' style='width:50px' id='baslangicSaati' value='" + $.fullCalendar.formatDate(start, 'HH:mm') + "'/>");
        $(".CalendarForm").append("<label >Bitiş Tarihi</label>");
        $(".CalendarForm").append("<input type='text' name='bitisTarihi' class='Date' id='bitisTarihi' value='" + $.fullCalendar.formatDate(end, 'dd.MM.yyyy') + "'/>");
        $(".CalendarForm").append("<input type='text' name='bitisSaati' class='Time' style='width:50px' id='bitisSaati' value='" + $.fullCalendar.formatDate(end, 'HH:mm') + "'/>");
        $(".CalendarForm").append("<label >Açıklama</label>");
        $(".CalendarForm").append("<textarea name='aciklama' id='aciklama' class='Text' style='width:325px;height:100px'>" + title + "</textarea>");
        $(".CalendarForm label").css("display", "block");

        $(".CalendarForm").attr("title", "Etkinlik Özellikleri");

        $.datepicker.setDefaults($.datepicker.regional["tr"]);
        $(".CalendarForm #baslangicTarihi").datepicker({
            showOtherMonths: true,
            selectOtherMonths: true
        });
        $(".CalendarForm #bitisTarihi").datepicker({
            showOtherMonths: true,
            selectOtherMonths: true
        });

        $(".CalendarForm").dialog({
            autoOpen: false,
            height: 300,
            width: 350,
            modal: true,
            buttons: {
                "Sil": function () {
                    if (!$(".CalendarForm #ajandaId").val())
                        $(".CalendarForm").dialog("close");
                    if (confirm("Bu etkinliği silmek istediğinize emin misiniz")) {
                        $.post('/Cloud/KullaniciAjanda.aspx?islem=Sil', { ajandaId: $(".CalendarForm #ajandaId").val() },
                            function () { $(widget.container.find(".portlet-widget")).fullCalendar("refetchEvents"); $(".CalendarForm").dialog("close"); }
                        );
                    }
                },
                "Kaydet": function () {
                    var dialog = $(this);
                    widget.saveEvent(widget, $(".CalendarForm #ajandaId").val(), $(".CalendarForm #baslangicTarihi").val(), $(".CalendarForm #baslangicSaati").val(),
                        $(".CalendarForm #bitisTarihi").val(), $(".CalendarForm #bitisSaati").val(), $(".CalendarForm #aciklama").val());
                }
            }
        });
        $(".CalendarForm").dialog("open");
    },

    saveEvent: function (widget, id, startDate, startTime, endDate, endTime, title) {
        $.post(
            '/Cloud/KullaniciAjanda.aspx?islem=Kaydet',
            {
                ajandaId: id,
                baslangicTarihi: startDate,
                baslangicSaati: startTime,
                bitisTarihi: endDate,
                bitisSaati: endTime,
                aciklama: title
            },
            function () {
                $(widget.container.find(".portlet-widget")).fullCalendar("refetchEvents");                
                $(".CalendarForm").dialog("close"); 
            }
        );
    }
}